package cn.wizzer.app.web.modules.controllers.platform.user.chat.group;

import cn.wizzer.app.user.modules.models.GroupInfo;
import cn.wizzer.app.user.modules.services.GroupInfoService;
import cn.wizzer.app.user.modules.services.GroupMemberService;
import cn.wizzer.app.user.modules.services.UserService;
import cn.wizzer.app.web.commons.utils.StringUtil;
import cn.wizzer.framework.base.Result;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

@IocBean
@At("/platform/user/group")
public class GroupController {
    private static final Log log = Logs.get();

    @Inject
    @Reference
    private GroupInfoService groupInfoService;

    @Inject
    @Reference
    private UserService userService;

    @Inject
    @Reference
    private GroupMemberService groupMemberService;

    @At("/")
    @Ok("beetl:/platform/user/chat/group/index.html")
    @RequiresPermissions("user.group.manage")
    public void index() {
    }

    /**
     * 查询（分页）
     *
     * @return Object
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("user.group.manage")
    public Object data(@Param("searchKeyword") String searchKeyword,
                       @Param("pageNumber") int pageNumber,
                       @Param("pageSize") int pageSize) {
        return Result.success().addData(groupInfoService.listPage(pageNumber, pageSize, searchKeyword));
    }

    /**
     * 添加群
     *
     * @return Object
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("user.group.edit")
    public Object add(@Param("..") GroupInfo groupInfo) {
        groupInfo.setOpBy(StringUtil.getPlatformUid());
        groupInfoService.add(groupInfo);
        return Result.success();
    }

    /**
     * 编辑群
     *
     * @return Object
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("user.group.edit")
    public Object edit(@Param("..") GroupInfo groupInfo) {
        groupInfoService.edit(groupInfo, StringUtil.getPlatformUid());
        return Result.success();
    }

    /**
     * 删除群
     *
     * @return Object
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("user.group.edit")
    public Object del(String id) {
        groupInfoService.update(Chain.make("delFlag", 1), Cnd.where("id", "=", id));
        return Result.success();
    }

    /**
     * 查询未入该群用户
     *
     * @return Object
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("user.group.manage")
    public Object memberList(@Param("groupId") String groupId,
                             @Param("searchKeyword") String searchKeyword,
                             @Param("pageNumber") int pageNumber,
                             @Param("pageSize") int pageSize) {
        return Result.success().addData(groupInfoService.memberList(pageNumber, pageSize, groupId, searchKeyword));
    }

    /**
     * 批量添加群成员
     *
     * @return Object
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("user.group.edit")
    public Object batchAdd(String[] ids, String groupId) {
        groupMemberService.batchAdd(ids, groupId);
        return Result.success();
    }

}
